//
//  movieViewController.m
//  ui豆瓣
//
//  Created by lirui on 15/12/23.
//  Copyright © 2015年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "movieViewController.h"
#import "movieTableViewCell.h"
#import "moviemodel.h"
#import "UIImageView+WebCache.h"
#import "moviecollectionViewController.h"
#import "collectionViewController.h"
@interface movieViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, retain)UITableView *tableview;
@property(nonatomic, retain)NSMutableArray *movArr;
@end

@implementation movieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [ UIColor whiteColor];
    self.tableview= [[ UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableview.backgroundColor = [ UIColor whiteColor];
    [self.view addSubview:self.tableview];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    self.navigationItem.title = @"电影";
    //导航栏适应边界适应屏幕
    self.tableview.contentInset = UIEdgeInsetsMake(0, 0,64, 0);
    
    //注册
    [self.tableview registerClass:[movieTableViewCell class ] forCellReuseIdentifier:@"cell"];
    //遍历数据源
    // 数据处理的目的 : 数组+字典 -> 数组+对象
                //隐藏分割线
 self.tableview.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
        //设置导航栏的图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"bg.png"]forBarMetrics:UIBarMetricsDefault];
        
    
    //导航栏右上角图标
    self.navigationItem.rightBarButtonItem = [[ UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_nav_collection@2x"] style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    self.navigationController.navigationBar.tintColor = [ UIColor blackColor];
    [self datahandeler];
}

-(void)datahandeler
{
    NSString *str = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    NSString *urlstr =[ str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:str]];
    NSURL *url =[ NSURL URLWithString:urlstr];
    NSURLRequest *request =[ NSURLRequest requestWithURL:url];
    NSURLSession *session = [ NSURLSession sharedSession];
    NSURLSessionDataTask *task =[ session  dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        dispatch_async(dispatch_get_main_queue(), ^{
            id result = [ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSArray *arr = result[@"result"];
            self.movArr = [NSMutableArray array];
            self.movArr =[moviemodel   modelHanderWithArray:arr];
            
            [self.tableview reloadData];
        });
    }];
    
    [task resume];
}


-(void)right:(UIBarButtonItem *)right
{collectionViewController *collection =[[ collectionViewController alloc] init];
    [self.navigationController pushViewController:collection animated:YES];
    
}
//行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _movArr.count;
}
//赋值
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    movieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    moviemodel *m = _movArr[ indexPath.row];
    cell.titlelabel.text = m.movieName;
    //使用SDwebimageView第三方 加载网络图片
    //string -> url -> 添加到imageview上
    
    [cell.movieimg sd_setImageWithURL:[NSURL URLWithString:m.pic_url]];
    return cell;
}
//行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    moviecollectionViewController *MC = [[ moviecollectionViewController alloc]init];
    moviemodel *m = _movArr[ indexPath.row];
    MC.Mov = m;
    NSLog(@"~~~~~~%@",MC.Mov);
    [self.navigationController pushViewController:MC animated:YES];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
